define(['ojs/ojcore', 'knockout', 'jquery','ojs/ojmodule', 'ojs/ojrouter','ojs/ojpagingcontrol'], function(oj, ko, $) {
    function servelistt() {
        var serveModule = {
        	check_flag:false,
            defaulePage:1,
            currentPage:ko.observable(1),
            totalPage:10,
            ajax_flag:true,
        	selectedServe: ko.observable({
        		assetId:'001',
				ipAddress:'192.168.1.1',
				userName:'gy',
				password:'1234',
        	}),
            getCdnCounts:function(){
                return 95;
                this.totalPage = 10;
            },
            pageBefore:function(){
                var a = this.currentPage();
                a>1?a--:1;
                this.currentPage(a);
                this.ajax_flag?this.getServeList():'' ;
                this.ajax_flag = a==1?false:true;
                a = null;
                // console.log(this.currentPage());
            },
            pageAfter:function(){
                var a = this.currentPage();
                var b = this.totalPage;
                a = a<b?a++:b;
                this.currentPage(a);
                this.ajax_flag?this.getServeList():'' ;
                this.ajax_flag = a==b?false:true;
                a = null;
                b = null;
                // console.log(this.currentPage());
            },
            pageFirst:function(){
                this.currentPage(1);
                this.getServeList();
            },
            pageLast:function(){
                this.currentPage(this.totalPage);
                this.getServeList() ;
            },
            getServeList:function(){
                var self = this;
                $.ajax({
                     type:"get",
                     url: "js/json/serve.json",
                     data:{currentPage:self.currentPage},
                     success: function(data){
                        console.log(data);
                        self.servelist(data);
                     }
                 });
            },
        	servelist:ko.observableArray([
        			{
        				assetId:'001',
        				ipAddress:'192.168.1.1',
        				userName:'gy',
        				password:'1234',
        			},
        			{
        				assetId:'002',
        				ipAddress:'192.168.1.2',
        				userName:'gy',
        				password:'1234',
        			},
        			{
        				assetId:'003',
        				ipAddress:'192.168.1.3',
        				userName:'gy',
        				password:'1234',
        			},
        			{
        				assetId:'004',
        				ipAddress:'192.168.1.4',
        				userName:'gy',
        				password:'1234',
        			}
        		]),
        	searchServeList:function(){
                // alert($('#search_value').val());
                var searchValue = $('#search_value').val();
                var self = this;
                $.ajax({
                    type:'get',
                    url:'',
                    data:{ser: searchValue},
                    success:function(data){
                        // self.servelist(data);
                        alert('查询成功');
                    }
                })
        	},
        	checkall:function(){ 
        		if(this.check_flag){
        			$('.item_selected:checked').prop('checked',false);
        			this.check_flag = false;
        		}else{
        			$('.item_selected').prop('checked',true);
        			this.check_flag = true;
        		}
        		console.log($('.item_selected:checked'));
        	},
        	diagoClose:function(){
        		$('#serve_add').hide('fast');
        		$('#serve_change').hide('fast');
                $('#serve_delete').hide('fast');
        		$('#mask').hide('fast');
        	},
        	addServe:function(){
        		$('#serve_add').show('fast');
        		$('#mask').show('fast');
                $('#add_serve').on('click',function(){
                    console.log('添加');
                    $.ajax({
                        type:'get',
                        url:'',
                        data:{
                            a:'',
                        },
                        success:function(data){
                            $('#serve_add').hide('fast');
                            $('#mask').hide('fast');
                            alert('添加成功');
                            window.location.reload();
                        }
                    });
                });
        	},
        	changeServe:function(){
        		switch($('input:checked').length){
        			case 1: change(); break;
        			case 0: alert('请选择一个服务器'); break;
        			default : alert('修改时只能选择一个服务器操作'); 
        		}
        		function change(){
	        		$('#mask').show('fast')
	        		$('#serve_change').show('fast');
	        		var index = $('input:checked').index('.item_selected');
	        		var $curdom = $('.serve_item').eq(index).find('span');
	        		$('#cassetId').val($curdom[1].innerHTML);
	        		$('#cIPAddress').val($curdom[2].innerHTML);
	        		$('#cusername').val($curdom[3].innerHTML);
	        		$('#cpassword').val($curdom[4].innerHTML);
        		}
        	},
        	deleteServe:function(){
        		switch($('input:checked').length){
        			case 0: alert('请选择一个服务器'); break;
        			default : change(); 
        		}
        		function change(){
	        		$('#mask').show('fast')
	        		$('#serve_delete').show('fast');
                    var ipAddressList = [];
                    for(var i = 0 ; i<$('.item_selected:checked').length;i++){
                        var a = $('.item_selected:checked').eq(i).index('.item_selected');
                        console.log(a);
                        ipAddressList.push($('.ipaddress').eq(a).html());
                    }
                    var html = '';
                    for(var i = 0 ,j = ipAddressList.length; i < j ; i++){
                        html += ipAddressList[i] + '<br>';
                    }
                    $('#serve_delete .delete_serlist').html(html);
                    console.log(ipAddressList);
                    $('#delete_serve').on('click',function(){
                        $.ajax({
                            type:'get',
                            url:'',
                            data:{
                                iplist: ipAddressList +''
                            },
                            success:function(data){

                            }
                        });
                    })
        		}
        	}
        };
        	console.log(11111);
        serveModule.datasource = new oj.ArrayTableDataSource(serveModule.servelist, {idAttribute: 'assetId'});
        this.handleAttached =function(){
        	console.log('handleAttached');
            serveModule.getServeList(this.currentPage);
            ko.cleanNode(document.getElementById('serve_list'));
            ko.applyBindings(serveModule,document.getElementById('serve_list'));
            ko.applyBindings(serveModule,document.getElementById('table'));
        }

    }
    // ko.applyBindings(homeViewModule, document.getElementsByClassName('content')[0]);
    return servelistt;
});
